
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html id="designdetector-com" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="/css/admin.css" rel="stylesheet" type="text/css"></link>
<script type="text/javascript" src="/js/jquery.js"> </script>
<title>专题管理</title>
</head>
<body>
  <div class="main">
    id <input id="new-id" type="text" value="" size="10">
    title <input id="new-title" type="text" value="" size="40">
    picture <input id="new-picture" type="text" value="" size="40">
    <a id="new-topic" href="#">新建专题</a>
    <ul>
    <?php 

function GetArticleMeta($aid) {
  $meta = null;
  if (!($meta = apc_fetch(sprintf('article-meta-%d', $aid)))) {
    $sql=sprintf("SELECT a.id AS a_id, a.title AS a_title, a.url AS a_url, a.category AS a_category, a.create_time AS a_create_time,
      u.id AS u_id, u.name AS u_name, u.title AS u_title, u.url AS u_url
      FROM articles AS a, authors AS u WHERE a.author = u.id AND a.id = %d", $aid);
    // print $sql;
    $result = mysql_query($sql);
    $meta = mysql_fetch_array($result);
    if ($meta && $meta['a_title']) {
      apc_add(sprintf('article-meta-%d', $aid), $meta, 3600 * 2);
    }
  }
  return $meta;
}

    $conn = mysql_connect("127.0.0.1","root","");
    mysql_select_db("xingtan");

    $sql=sprintf("SELECT id, state, picture, title, articles, update_time FROM topics");
    $result = mysql_query($sql);
    print count($result);

    while($row = mysql_fetch_array($result)) {
      printf('<li>id <span id="topic-id">%d</span>', $row['id']);
      printf(' state <input id="topic-state" style="border:1px solid #cbc;" type="text" size="3" value="%d"/>', $row['state']);
      print(' title <input id="topic-title" type="text" size="40" value="'. $row['title']. '"/>' 
        . ' picture <input id="topic-picture" type="text" size="40" value="'. $row['picture']. '"/>');
      printf('articles <input id="topic-articles" style="border:1px solid #cbc;" type="text" size="60" value="%s"/>', $row['articles']);

      printf('<span><a href="#nogo" class="update-button" id="%d">update</a> <a href="#nogo" class="del-button" id="%d">delete</a></span>', $row['id'], $row['id']);
      $articles = split(",", $row['articles']);
      print(" articles:");
      foreach($articles as $a) {
        $ameta = GetArticleMeta($a);
        printf(' <a title="%s" href="http://www.slowrose.com/a/%s.html">%s</a>', $ameta['a_title'], $a, $a);
      }
      print('</li>');
    }
    mysql_close($conn);
    ?>
    </ul>
	</div>
<script type="text/javascript">
    $('.del-button').click(
      function() {
        // alert($(this).attr('id'));
        $.ajax({
          url: "/topic-del.php?id=" + $(this).attr('id'),
          context : $(this).parent(),
          success : function() {
            $(this).text("删除成功");
          }
        });
      }
    );
    
    $('.update-button').click(
      function() {
        var li = $(this).parent().parent();
        $.ajax({
          url: "/topic-update.php?id=" + $('#topic-id', li).text()
            + '&state=' + + $('#topic-state', li).val()
            + '&t=' + encodeURIComponent($('#topic-title', li).val())
            + '&p=' + encodeURIComponent($('#topic-picture', li).val())
            + '&as=' + encodeURIComponent($('#topic-articles', li).val()),
          context : $(this),
          success : function(rsp_text) {
            alert(rsp_text);
          }, 
          error : function() {
            alert('更新失败');
          }
        });
      }
    );

    $('#new-topic').click(
      function() {
        var id = $('#new-id').val();
        var title = $('#new-title').val();
        var picture = $('#new-picture').val();
        if(id.length<=0 || picture.length <=0 || title.length <=0) {
          alert('字段不全');
          return;
        }
        $.ajax({
          url: "/topic-add.php?t=" + encodeURIComponent(title)
            + '&id=' + id + '&p=' + picture,
          context : $(this),
          success : function() {
             $(this).html("添加成功，请刷新页面");
          }, 
          error : function() {
            alert('添加失败');
          }
        });
      }
    );
</script>
</body>
